package com.app.newcsc.fragment.my.paytype;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.app.newcsc.R;
import com.app.newcsc.entity.PayAccountEntity;
import com.app.newcsc.fragment.my.paytype.addpaytype.AddBankFragment;
import com.app.newcsc.fragment.my.paytype.addpaytype.AddWXFragment;
import com.app.newcsc.fragment.my.paytype.addpaytype.AddZFBFragment;
import com.app.newcsc.utils.Utils;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class PayTypeItemViewModel extends ItemViewModel<PayTypeViewModel> {
    public ObservableField<PayAccountEntity> entity = new ObservableField<>();
    public Drawable drawableImg;

    public PayTypeItemViewModel(@NonNull PayTypeViewModel viewModel, PayAccountEntity entity) {
        super(viewModel);
        this.entity.set(entity);
        //ImageView的占位图片，可以解决RecyclerView中图片错误问题
        drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    public String setName()
    {
        PayAccountEntity mPayAccountEntity = entity.get();
//        if(mPayAccountEntity.type == 1)
//        {
//            return mPayAccountEntity.branch;
//        }
//        return "";
        return mPayAccountEntity.name;
    }
    public String setAccount()
    {
        PayAccountEntity mPayAccountEntity = entity.get();
        return mPayAccountEntity.account;
    }

    public String setType()
    {
        PayAccountEntity mPayAccountEntity = entity.get();
        if(mPayAccountEntity.type == 1)
        {
            return "银行卡";
        }
        else if(mPayAccountEntity.type == 2)
        {
            return "支付宝";
        }
        else if(mPayAccountEntity.type == 3)
        {
            return "微信";
        }
        return "";
    }
    public Drawable setImageType()
    {
        PayAccountEntity mPayAccountEntity = entity.get();
        if(mPayAccountEntity.type == 1)
        {
            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_bank);
        }
        else if(mPayAccountEntity.type == 2)
        {
            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_zfb);
        }
        else if(mPayAccountEntity.type == 3)
        {
            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_wx);
        }
        return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_bank);
    }
//    @BindingAdapter("imageUrl")
//    public void bindImageUrl(ImageView imageView) {
////        GlideApp.with(imageView.getContext())
////                .load(url)
////                .into(imageView);
//
//        PayAccountEntity mPayAccountEntity = entity.get();
//        if(mPayAccountEntity.type == 1)
//        {
//            imageView.setImageResource(R.mipmap.paytype_bank);
//        }
//        else if(mPayAccountEntity.type == 2)
//        {
//            imageView.setImageResource(R.mipmap.paytype_zfb);
//        }
//        else if(mPayAccountEntity.type == 3)
//        {
//            imageView.setImageResource(R.mipmap.paytype_wx);
//        }
//    }
    public BindingCommand onClickEdit = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
    //            Log.e("TAG", getPosition()+"=============onClickBuySell=================");
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
    //            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
    //            ToastUtils.showShort(entity.get().getName());

            viewModel.editItemLiveData.setValue(PayTypeItemViewModel.this);
        }
    });
    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(!Utils.isLogin())
            {
                return;
            }
            Bundle mBundle = new Bundle();
            mBundle.putSerializable("entity", entity.get());
            Log.e("TAG","=============image==========="+entity.get().image);
            if(entity.get().type == 1)
            {
                viewModel.startContainerActivity(AddBankFragment.class.getCanonicalName(), mBundle);
            }
            else if(entity.get().type == 2)
            {
                viewModel.startContainerActivity(AddZFBFragment.class.getCanonicalName(), mBundle);
            }
            else if(entity.get().type == 3)
            {
                viewModel.startContainerActivity(AddWXFragment.class.getCanonicalName(), mBundle);
            }
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());
        }
    });
//    /**
//     * 可以在xml中使用binding:currentView="@{viewModel.titleTextView}" 拿到这个控件的引用, 但是强烈不推荐这样做，避免内存泄漏
//     **/
//    private TextView tv;
//    //将标题TextView控件回调到ViewModel中
//    public BindingCommand<TextView> titleTextView = new BindingCommand(new BindingConsumer<TextView>() {
//        @Override
//        public void call(TextView tv) {
//            NetWorkItemViewModel.this.tv = tv;
//        }
//    });
}
